package demo.practice.binary_search;

public class P1150 {

    public static void main(String[] args) {
        P1150 p1150= new P1150();
        p1150.isMajorityElement(new int[]{2,4,5,5,5},5);
    }

    public boolean isMajorityElement(int[] nums, int target) {
        int min = 0;
        int max = nums.length - 1;

        int mid = min + (max - min) / 2;

        if (nums[mid] == target) {
            //已经找到
            int pos = mid;
            int times = 1;
            while (pos<nums.length-1 && nums[++pos] == target)
                times++;

            if (times > nums.length / 2)
                return true;
            else if (mid-(nums.length / 2 - times)>=0&&  nums[mid-(nums.length / 2 - times)]==target ) {
                //正向已经有times次了，负方向则至少有 nums.length / 2 - times次，当然负方向不能越界

                return true;
            } else
                return false;

        } else if (nums[mid] > target) {
            //target在前面
            max = mid - 1;

        } else {
            //target在后面
            min = mid + 1;
        }

        //如果没找到，直接返回false
        return false;
    }
}
